/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pokerhandcheck;
import java.util.*;
/**
 *
 * @author cbowman
 */
public class Hand {

    Card card1;
    Card card2;
    Card card3;
    Card card4;
    Card card5;
    int rank;
    String rnk;
    int primary = 0;
    int secondary = 0;
    int tertiary = 0;
    int quaternary = 0;
    int quinary = 0;
    int[] cards = new int[5];
    int[] suits = new int[5];
    int[] counts = new int[13];

    public Hand(Card c1, Card c2, Card c3, Card c4, Card c5){
        card1 = new Card(c1.value,c1.suit);
        card2 = new Card(c2.value,c2.suit);
        card3 = new Card(c3.value,c3.suit);
        card4 = new Card(c4.value,c4.suit);
        card5 = new Card(c5.value,c5.suit);
    }

    public void CheckHand(){
        cards[0] = card1.value;
        cards[1] = card2.value;
        cards[2] = card3.value;
        cards[3] = card4.value;
        cards[4] = card5.value;
        suits[0] = card1.suit;
        suits[1] = card2.suit;
        suits[2] = card3.suit;
        suits[3] = card4.suit;
        suits[4] = card5.suit;
        Arrays.sort(cards);
        boolean pr = false;
        boolean twopr = false;
        boolean trps = false;
        boolean fulhs = false;
        boolean qds = false;
        boolean str8 = false;
        boolean flsh = false;
        boolean strflsh = false;
        CountHand();
        if(isPair()){
            pr = true;
            rank = 2;
            rnk = new String("Pair");
            for(int i = 0; i < 13; i++){
                if(counts[i] == 2){
                    primary = i + 2;
                    i = 13;
                }
            }
            int temp = 0;
            for(int i = 0; i < 13; i++){
                if(counts[i] == 1)
                    temp = i + 2;
            }
            secondary = temp;
            System.out.println("You have a Pair of " + primary + "'s with a " + secondary + " kicker.");
        }
        if(isTwoPair()){
            twopr = true;
            rank = 3;
            rnk = new String("Two Pair");
            boolean b = false;
            for(int i = 0; i < 13; i++){
                if(!b){
                    if(counts[i] == 2){
                        primary = i + 2;
                        b = true;
                    }
                }
                else{
                    if(counts[i] == 2){
                        secondary = i + 2;
                        i = 13;
                    }
                }
            }
            int temp = 0;
            for(int i = 0; i < 13; i++){
                if(counts[i] == 1)
                    temp = i + 2;
            }
            tertiary = temp;
            System.out.println("You have Two Pair, " + primary + "'s and " + secondary + "'s, with a " + tertiary + " kicker.");
        }
        if(isTrips()){
            trps = true;
            rank = 4;
            rnk = new String("Trips");
            for(int i = 0; i < 13; i++){
                if(counts[i] == 3){
                    primary = i + 2;
                    i = 13;
                }
            }
            int temp = 0;
            for(int i = 0; i < 13; i++){
                if(counts[i] == 1)
                    temp = i + 2;
            }
            secondary = temp;
            System.out.println("You have Trip " + primary + "'s with a " + secondary + " kicker.");
        }
        if(isFullHouse()){
            fulhs = true;
            rank = 7;
            rnk = new String("Full House");
            boolean b = false;
            for(int i = 0; i < 13; i++){
                if(counts[i] == 3){
                    primary = i + 2;
                }
                if(counts[i] == 2){
                    secondary = i + 2;
                }
            }
            System.out.println("You have a Full House, " + primary + "'s full of " + secondary + "'s.");
        }
        if(isQuads()){
            qds = true;
            rank = 8;
            rnk = new String("Quads");
            for(int i = 0; i < 13; i++){
                if(counts[i] == 4){
                    primary = i + 2;
                    i = 13;
                }
            }
            int temp = 0;
            for(int i = 0; i < 13; i++){
                if(counts[i] == 1)
                    temp = i + 2;
            }
            secondary = temp;
            System.out.println("You have Quad " + primary + "'s with a " + secondary + " kicker.");
        }
        if(!pr && !twopr && !trps && !fulhs && !qds){
            if(isStraight() && !isFlush()){
                str8 = true;
                rank = 5;
                rnk = new String("Straight");
                primary = cards[4];
                System.out.println("You have a Straight to a " + primary);
            }
            if(!isStraight() && isFlush()){
                flsh = true;
                rank = 6;
                rnk = new String("Flush");
                primary = cards[4];
                secondary = cards[3];
                tertiary = cards[2];
                quaternary = cards[1];
                quinary = cards[0];
                System.out.println("You have a Flush with high card " + primary + " : " + toString());
            }
            if(isStraight() && isFlush()){
                strflsh = true;
                rank = 9;
                rnk = new String("Straight Flush");
                primary = cards[4];
                System.out.println("You have a Straight Flush to a " + primary);
            }
        }
        if(!pr && !twopr && !trps && !fulhs && !qds && !str8 && !flsh){
            rank = 1;
            rnk = new String("High Card");
            primary = cards[4];
            secondary = cards[3];
            tertiary = cards[2];
            quaternary = cards[1];
            quinary = cards[0];
            System.out.println("You have a High card of " + primary + " : " + toString());
        }
    }

    public void CountHand(){
        for(int i = 2; i < 15; i++){
            int counter = 0;
            for(int j = 0; j < cards.length; j++){
                if(cards[j] == i)
                        counter++;
            }
            counts[i-2] = counter;
        }
    }

    public boolean isPair(){
        boolean b = false;
        int counter = 0;
        boolean imp = false;
        for(int i = 0; i < 13; i++){
            if(counts[i] == 2)
                counter++;
            if((counts[i] == 3) || (counts[i] == 4))
                imp = true;
        }
        if((counter == 1) && !imp)
            b = true;
        return b;
    }

    public boolean isTwoPair(){
        boolean b = false;
        int counter = 0;
        boolean imp = false;
        for(int i = 0; i < 13; i++){
            if(counts[i] == 2)
                counter++;
            if((counts[i] == 3) || (counts[i] == 4))
                imp = true;
        }
        if((counter == 2) && !imp)
            b = true;
        return b;
    }

    public boolean isTrips(){
        boolean b = false;
        int counter = 0;
        boolean imp = false;
        for(int i = 0; i < 13; i++){
            if(counts[i] == 3)
                counter++;
            if((counts[i] == 2) || (counts[i] == 4))
                imp = true;
        }
        if((counter == 1) &&!imp)
            b = true;
        return b;
    }

    public boolean isFullHouse(){
        boolean b = false;
        int pair_counter = 0;
        int trip_counter = 0;
        for(int i = 0; i < 13; i++){
            if(counts[i] == 2)
                pair_counter++;
            if(counts[i] == 3)
                trip_counter++;
        }
        if((pair_counter == 1) && (trip_counter == 1))
            b = true;
        return b;
    }

    public boolean isQuads(){
        boolean b = false;
        int counter = 0;
        for(int i = 0; i < 13; i++){
            if(counts[i] == 4)
                counter++;
        }
        if(counter == 1)
            b = true;
        return b;
    }

    public boolean isStraight(){
        boolean b = true;
        for(int i = 1; i < 5; i++){
            if(cards[i] - cards[i-1] != 1){
                b = false;
                i = 5;
            }
        }
        return b;
    }

    public boolean isFlush(){
        boolean b = true;
        int cmp = suits[0];
        for(int i = 1; i < 5; i++){
            if(suits[i] != cmp){
                b = false;
                i = 5;
            }
        }
        return b;
    }

    public String toString(){
        String s = new String("{" + card1.toString() + ", "
                                  + card2.toString() + ", "
                                  + card3.toString() + ", "
                                  + card4.toString() + ", "
                                  + card5.toString() + "}");
        return s;
    }
}
